<template>
  <div class="member-task-operation">
    <operation-title title="优惠券" :icon="TitleIcon" />
    <div class="divider"></div>
    <div class="func">
      <p class="attr">选择风格</p>
      <div class="adjust-item">
        <div class="style-btn" @click="dialogFormVisible = true">修改风格</div>
        <span class="style-word">当前风格{{ config.pageConfig.style }}</span>
      </div>
    </div>

    <div class="divider"></div>
    <div class="func">
      <p class="attr">背景设置</p>
      <div class="adjust-item">
        <span>组件背景</span>
        <el-radio-group v-model="config.pageConfig.cptType">
          <el-radio :label="1">背景色</el-radio>
          <el-radio :label="2">背景图</el-radio>
        </el-radio-group>
      </div>
      <div class="adjust-item" v-show="config.pageConfig.cptType == 1">
        <span></span>
        <el-color-picker v-model="config.pageConfig.bgColor" />
        <span class="color-value">{{ config.pageConfig.bgColor }}</span>
      </div>
      <div class="add-item-input" v-show="config.pageConfig.cptType == 2">
        <p></p>
        <h4>建议图片宽度355px，高度120px</h4>
        <div class="upload-img" v-if="config.pageConfig.bgImg">
          <el-image style="width: 100%; height: 100%" :src="config.pageConfig.bgImg" fit="contain"></el-image>
          <div class="upload-img_cover">
            <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
              @success="uploadSuccess">
              <el-button size="small" @click="currentType = 'bgImg'">替换</el-button>
            </Upload>
          </div>
        </div>
        <Upload v-else v-model="config.pageConfig.bgImg" file-path="act/" list-type="picture-card">
          <el-icon>
            <Plus />
          </el-icon>
        </Upload>
      </div>
    </div>

    <div class="divider"></div>
    <div class="func">
      <p class="attr">展示方式</p>
      <ul class="common-ul">
        <li :class="config.pageConfig.showStyle == 1 ? 'active' : ''" @click="config.pageConfig.showStyle = 1">
          <img src="../../../../../../assets/app/other/style/rightstyle1.png" alt="">
        </li>
        <li :class="config.pageConfig.showStyle == 2 ? 'active' : ''" @click="config.pageConfig.showStyle = 2">
          <img src="../../../../../../assets/app/other/style/rightstyle2.png" alt="">
        </li>
      </ul>
    </div>
    <!-- 卡券列表 -->
    <div class="cards-box">
      <div class="card-name">优惠券({{ config.pageConfig.couponList.length }})</div>
      <div class="card-list">
        <div class="card-item" v-for="(item, index) in config.pageConfig.couponList">
          <div class="item-row"><span class="row-lable">名称</span><span class="row-text">{{ item.couponName }}</span></div>
          <div class="item-row"><span class="row-lable">优惠规则</span> 
           <span class="row-text" v-if="item?.couponType === 1">
              消费满{{ item?.couponDiscount?.fullPrice }}，立减{{ item?.couponDiscount?.reducePrice }}元
            </span>
            <span class="row-text" v-if="item?.couponType === 2">
              消费满{{ item?.couponDiscount?.fullPrice }}，可享受折扣{{ item?.couponDiscount?.discount }}折（折扣比例:原价为1，0.99即为九九折）
            </span>
            <span class="row-text" v-if="item?.couponType === 3">
              使用范围商品任选一件兑换
            </span>
          </div>
          <div class="item-row" style="margin-bottom: 0;"><span class="row-lable">使用范围</span><span class="row-text">{{
            getRangeLable(item) }}</span></div>
          <div class="move-btns">
            <el-icon v-show="index != 0" class="move-icon" title="上移" @click="moveTo('up', index)">
              <Top />
            </el-icon>
            <el-icon v-show="index != config.pageConfig.couponList.length - 1" style="cursor: pointer" title="下移"
              @click="moveTo('down', index)">
              <Bottom />
            </el-icon>
          </div>
          <el-popconfirm confirm-button-text="是" cancel-button-text="否" :title="config.pageConfig.couponList.length > 1
            ? '确定删除吗？'
            : '只有一个不可删除'
            " @confirm="sliceItem(index)">
            <template #reference>
              <el-icon class="sure-close">
                <Close />
              </el-icon>
            </template>
          </el-popconfirm>
        </div>
        <div class="card-item add-item" @click="addItem">
          <p>+添加</p>
        </div>
      </div>
    </div>

    <div class="divider"></div>
    <div class="func">
      <p class="attr">边距</p>
      <div class="adjust-item">
        <span>上边距</span>
        <el-slider v-model="config.pageConfig.marginTop" :min="0" :max="50" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>下边距</span>
        <el-slider v-model="config.pageConfig.marginBottom" :min="0" :max="50" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>左右边距</span>
        <el-slider v-model="config.pageConfig.margin" :min="0" :max="20" show-input size="small" />px
      </div>
    </div>


    <div class="divider"></div>
    <div class="func">
      <p class="attr">圆角设置</p>
      <div class="adjust-item">
        <span>上圆角</span>
        <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusTop" show-input size="small" />px
      </div>
      <div class="adjust-item">
        <span>下圆角</span>
        <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusBottom" show-input size="small" />px
      </div>
    </div>
    <Dialog class="dialog-form" v-model="dialogFormVisible" title="选择风格" width="60%" :before-close="resetForm">
      <div class="icon-div-container">
        <div class="icon-div" v-for="(item, index) in styleList" :key="index" @click="submitForm(item.id)">
          <img :src='item.img' />
          <span style="margin-top: 8px;"> {{ `风格${item.id}` }} </span>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="resetForm()">取消</el-button>
        </span>
      </template>
    </Dialog>
    <CouponSelector v-model:show="showSelect" @confirm="selectCoupon" />
  </div>
</template>

<script lang="ts">
import OperationTitle
  from "@/views/activity/actApp/components/default/operationComponents/operationTitle/OperationTitle.vue";
import CouponSelector from "@/views/pointsMall/components/CouponSelect.vue";
import {getCouponListOnline} from "@/api/actapp"
export default {
  name: "couponOperation",

};
</script>

<script setup lang="ts">
import TitleIcon from "@/assets/app/icon/coupon.png";
import style1png from '@/assets/app/other/style/coupon_style1.png'
import style2png from '@/assets/app/other/style/coupon_style2.png'
import style3png from '@/assets/app/other/style/coupon_style3.png'
import { useMallConfig } from "@/views/pointsMall/compositions/useMallConfig";
const config = defineProps({
  pageConfig: {
    type: Object,
    default: {
      style: 1,//风格
      cptType: 1, // 组件背景类型
      bgColor: "#FFFFFF",//背景色
      bgImg: "", // 背景图
      showStyle: 1,//展示方式
      couponList: [],//卡券列表
      marginTop: 10,
      marginBottom: 10,
      margin: 10,
      radiusTop: 4,
      radiusBottom: 4,
    },
  },
});

const styleList = [{ id: 1, img: style2png }, { id: 2, img: style3png }, { id: 3, img: style1png }]

const submitForm = (id: number) => {
  config.pageConfig.style = id;
  dialogFormVisible.value = false;
}

const currentType = ref("");

const fileList = ref([])
const uploadSuccess = (val: { name: string, url: string }) => {
  config.pageConfig.bgImg = val.url
}

const dialogFormVisible = ref(false)
const resetForm = () => {
  dialogFormVisible.value = false;
}

const sliceItem = (index: number) => {
  config.pageConfig.couponList.splice(index, 1)
}


const moveTo = (type: 'up' | 'down', index: number) => {
  const list = [...config.pageConfig.couponList]
  const item = list[index]
  //上移
  if (type == 'up') {
    const preItem = list[index - 1]
    list[index - 1] = item
    list[index] = preItem
  }
  //下移
  if (type == 'down') {
    const nextItem = list[index + 1]
    list[index] = nextItem
    list[index + 1] = item
  }
  config.pageConfig.couponList = [...list]
}
//记录已添加的卡券
const list=new Set()

const showSelect = ref(false)
const addItem = () => {
  showSelect.value = true
}
const getRangeLable = (item: any) => {
  if (item) {
    if (item.useRange == 1) {
      return `限${item.businessName}活动可用`
    }
    if (item.useRange == 2) {
      return '商城可用'
    } if (item.useRange == 3) {
      return '商城部分商品可用'
    }
    if (item.useRange == 4) {
      return `商城部分分类可用`
    }
  }
}

const selectCoupon = (val:any) => {
  val.couponDiscount=val.couponDiscount?JSON.parse(val.couponDiscount):{}
  if(!list.has(val.id)){
    list.add(val.id)
    config.pageConfig.couponList.push(val)
  }
  showSelect.value = false
}
let flag=true
const getCouponList = async(list)=>{
  flag=false
  const ids=list.map(i=>i.id)
  if (ids.length){
    const res = await getCouponListOnline({
      ids
    })
    if (res.code == 200){
      let arr=[]
    //获取券上架状态
      const data = res.payload || []
     
      for (let i = 0;i < data.length;i++){
        for (let j = 0;j < list.length;j++){
          if (data[i].id == list[j].id)
          if(data[i].onlineStatus)
            arr.push(list[j])
          }
      }
      config.pageConfig.couponList=[...arr]
      }
    } 
  }
  

const { mallConfig } = useMallConfig();
watch(()=>config.pageConfig.couponList,(val)=>{
  val.length&&flag&&getCouponList(val)
},{immediate:true})

let emits = defineEmits<{ (e: "changeStyle", configStyle: Object): void }>();
watch(config.pageConfig, () => {
  config.pageConfig.mallId= mallConfig.value.id,
  emits("changeStyle", config.pageConfig);
});

</script>
<style lang="scss" scoped>
.member-task-operation {
  .title {
    padding: 8px 23px;
    overflow: hidden;
    border-bottom: 5px solid #f6f7f9;
    display: flex;
    position: relative;
    height: 50px;
    box-sizing: border-box;

    img {
      position: absolute;
      left: 0;
      top: -5px;
    }

    h5 {
      font-size: 16px;
      font-weight: 700;
      position: absolute;
      left: 70px;
      top: 0;
      line-height: 50px;
    }
  }

  .func {
    border-bottom: 5px solid #f6f7f9;
    padding: 20px 16px;
    font-size: 16px;

    .color-value {
      display: inline-block;
      width: 200px;
      text-align: left;
      margin-left: 5px;
    }

    .attr {
      line-height: 1;
      color: #47565d;
      font-weight: bolder;
      margin-bottom: 20px;
    }

    ul.common-ul {
      list-style: none;
      display: flex;
      align-items: center;
      justify-content: center;

      li {
        flex: 1;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 4px 0 0 4px;
        border: 2px solid #ededed;
        border-collapse: collapse;

        &.active {
          border: 2px solid #fb6638;
        }

        img {
          max-width: 100% !important;
          max-height: 100% !important;
        }
      }
    }

    h4 {
      position: relative;
      padding: 8px 16px 8px 16px;
      border-radius: 6px;
      color: red;
      font-size: 16px;
      font-weight: 600;
      line-height: 22px;
      margin-bottom: 10px;
      border: 1px solid #d8e8fc;
      background-color: #e5f0fe;
      width: 170px;
      margin-right: 10px;
      margin-top: 10px;
    }

    div.adjust-item {
      display: flex;
      align-items: center;
      margin-top: 3px;
      margin-bottom: 10px;

      span {
        width: 80px;
        text-align: right;
        margin-right: 15px;
        color: #666;
      }

      :deep(.el-slider) {
        width: 240px;
        margin-right: 3px;
        margin-left: 15px;
      }

      :deep(.el-slider__runway.show-input) {
        margin-right: 15px;
      }

      :deep(.el-input-number) {
        width: 100px;
        background: #f4f3f7;
        border: 1px solid #f4f3f7;
      }

      .style-btn {
        width: 240px;
        height: 36px;
        font-size: 12px;
        line-height: 36px;
        text-align: center;
        background-color: #001E70;
        color: #fff;
        margin-right: 5px;
        cursor: pointer;
      }

      .style-word {
        font-size: 12px;
      }
    }


  }

  .add-item-input {
    display: flex;
    align-items: center;
    margin-bottom: 20px;

    >p {
      width: 77px;
      text-align: right;
      margin-right: 8px;
    }

    .upload-img {
      width: 62px;
      height: 62px;
      margin-right: 10px;
      position: relative;

      :deep(.el-image) {
        width: 62px;
        height: 62px;
      }

      &:hover {
        .upload-img_cover {
          opacity: 1;
        }
      }

      .upload-img_cover {
        opacity: 0;
        position: absolute;
        transition: all 0.3s ease;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.7);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-evenly;
      }
    }

    :deep(.el-upload-list) {
      width: 62px;
      height: 62px;
      margin-right: 10px;
    }

    :deep(.el-upload--picture-card) {
      width: 62px;
      height: 62px;
    }

    // img {
    //   width: 40px;
    //   height: 40px;
    // }
    :deep(.el-color-picker) {
      margin-right: 10px;
    }
  }
}

.icon-div-container {
  display: flex;
  flex-wrap: wrap;

  .icon-div {
    padding: 12px;
    margin-right: 12px;
    margin-bottom: 12px;
    width: 520px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    background: #f4f3f7;
    border-radius: 8px;
    cursor: pointer;
    img{
      max-width: 100%;
    }
  }
}

.cards-box {
  padding: 15px;
  border-bottom: 5px solid #f6f7f9;

  .card-name {
    color: #47565d;
    font-weight: bolder;
  }

  .card-list {
    .card-item {
      position: relative;
      padding: 15px;
      margin: 10px 0;
      border-radius: 6px;
      background: #f4f3f7;
      color: #999;
    }
  }
}

.move-btns {
  position: absolute;
  top: 5px;
  right: 24px;

  .move-icon {
    margin-right: 4px;
    cursor: pointer;
  }

}

.add-item {
  display: flex;
  justify-content: center;
  cursor: pointer;
}

.sure-close {
  position: absolute;
  right: 5px;
  top: 5px;
  cursor: pointer;
}

.item-row {
  margin-bottom: 16px;
  display: flex;

  .row-lable {
    flex-shrink: 0;
    display: inline-block;
    width: 70px;
    margin-right: 10px;
  }
}
</style>
